import resources.Constants;

import registnet.records.source.ChecklistSource;
import registnet.records.Checklist;
import registnet.records.ICourse;
import registnet.records.CourseTaken;
import registnet.records.source.StudentSource;
import registnet.records.Student;

/* [start] 	initializes gui variables */
String _checklist = bundle.getProperty("registnet.module.student_records.macro.checklist");
 
String _actions = bundle.getProperty("registnet.module.student_records.macro.checklist.form.actions");
String _export = bundle.getProperty("registnet.module.student_records.macro.checklist.form.actions.export");
String _print = bundle.getProperty("registnet.module.student_records.macro.checklist.form.actions.print");

String _course_code = bundle.getProperty("registnet.module.student_records.macro.checklist.form.course_code");
String _course_title = bundle.getProperty("registnet.module.student_records.macro.checklist.form.course_title");
String _units = bundle.getProperty("registnet.module.student_records.macro.checklist.form.units");
String _grade = bundle.getProperty("registnet.module.student_records.macro.checklist.form.grade");
/* [end] */



/* [start]	creates a checklist for the student */
Student student = new StudentSource(DAOStudent.getStudentNumber()).getStudent();
Checklist checklist = new ChecklistSource(student).getChecklist();
/* [end] */



/* [start]	initializes the year-semester hashmap */
List courses = checklist.getCourses();
List ascendingKey = new Vector();
HashMap yearSemester = new HashMap();
for(int i=0; i<courses.size(); i++){
	ICourse ic = courses.get(i);
	int[] y_s = new int[2];
	y_s[0] = ic.getSemester();
	y_s[1] = ic.getYear();
	String key;
		if(y_s[0] != 3)
			key = Constants.MAP_YEAR_VALUE.get(y_s[1]) + " YEAR, "+ Constants.MAP_SEMESTER_VALUE.get(y_s[0]) + " SEMESTER";
		else
			key = Constants.MAP_YEAR_VALUE.get(y_s[1]) + " YEAR, "+ Constants.MAP_SEMESTER_VALUE.get(y_s[0]);
	if (!yearSemester.containsKey(key)){
		yearSemester.put(key, y_s);
		ascendingKey.add(key);
	}
}
/* [end] */



/* [start]	defines a renderer for the contents of the checklist */
private class ChecklistRenderer implements RowRenderer  {
	public void render(Row row, java.lang.Object data) {
		if (data[1] != null){
			new Label(data[0]).setParent(row);
			new Label(data[1]).setParent(row);
			new Label(data[2]).setParent(row);
			new Label(data[3]).setParent(row);
		}
	
		else if(data[0].equals("")){
			Label label = new Label(data[0]);
			label.setParent(row);
			row.setSpans("4");
			row.setHeight("25px");
			row.setStyle("border-top: 2px solid #4f7fbf; background-color: #ffffff; ");
	}
		
	else{
			Label label = new Label(data[0]);
			label.setStyle("font-weight: bold");
			label.setParent(row);
			row.setSpans("4");
			row.setStyle("border-top: 2px solid #4f7fbf; border-bottom: 2px solid #4f7fbf; background-color: #BCD2EF;");
		}
	}
}
/* [end] */

	
/* [start]	defines a renderer for the contents of the recommended courses */
private class RecommendedCoursesRenderer implements RowRenderer  {
	public void render(Row row, java.lang.Object data) {
		if (data[1] != null){
			new Label(data[0]).setParent(row);
			new Label(data[1]).setParent(row);
		}
	}
}
/* [end] */


/* [start]	retrieves the courses on the checklist and places them on the grid */
void retrieveCourses(){
	
	ListIterator iter = ascendingKey.listIterator();
	String[][] listModel = new String[courses.size() + (2*yearSemester.size())][4];
	int i=0;
	while(iter.hasNext()){
		String key = iter.next();
		int[] y_s = yearSemester.get(key);
		listModel[i][0] = key;
		i++;
		List semesterCourses = checklist.getCourses(y_s[0], y_s[1]);
		for(int j=0; j<semesterCourses.size(); j++){
			ICourse course = semesterCourses.get(j);
			listModel[i][0] = course.getCourseCode();
			listModel[i][1] = course.getCourseName();
			listModel[i][2] = course.getUnit() + "";
			if (!(course instanceof CourseTaken))
				listModel[i][3] = "-";
			else{
				if(course.getReExam() == 0)
					listModel[i][3] = Constants.MAP_GRADE_VALUE.get(course.getGrade()); 
				else
					listModel[i][3] = Constants.MAP_GRADE_VALUE.get(course.getReExam()); 
			}
			i++;
		}
		listModel[i][0] ="";
		i++;
	}
	
	ListModel strset = new SimpleListModel(listModel);
	checklist_g.setModel(strset);
	checklist_g.setRowRenderer(new ChecklistRenderer());
}

	
/* [start]	retrieves the courses on the checklist and places them on the grid */	
void retrieveRecommendedCourses(){
	List recommendedCourses = checklist.getRecommendedCourses();
	String[][] listModel = new String[recommendedCourses.size()][2];
	for(int i=0; i<recommendedCourses.size(); i++){
		ChecklistCourse course = recommendedCourses.get(i);
		listModel[i][0] = course.getCourseCode();
		listModel[i][1] = course.getCourseName();
	}
	ListModel strset = new SimpleListModel(listModel);
	recommendedCourses_g.setModel(strset);
	recommendedCourses_g.setRowRenderer(new RecommendedCoursesRenderer());
}
/* [end] */